import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

class StorePicture extends StatefulWidget {
  final List<File> fileList;
  final Function getImage;

  StorePicture({this.getImage, this.fileList});

  @override
  _StorePictureState createState() => _StorePictureState();
}

class _StorePictureState extends State<StorePicture> {
  int maxLength = 8; //图片最大数量
  @override
  Widget build(BuildContext context) {
    return StaggeredGridView.countBuilder(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      crossAxisCount: 3,
      mainAxisSpacing: setWidth(10),
      crossAxisSpacing: setWidth(10),
      staggeredTileBuilder: (i) {
        return StaggeredTile.fit(1);
      },
      itemCount: widget.fileList.length + 1,
      itemBuilder: (context, index) {
        return GestureDetector(onTap: () {}, child: _gridItem(index));
      },
    );
  }

  Widget _gridItem(int index) {
    if (index + 1 > maxLength) {
      return Container();
    }
    //点击选择图片
    if (widget.fileList.length == 0 || index == widget.fileList.length) {
      return Row(
        children: [
          GestureDetector(
            onTap: () {
              widget.getImage();
            },
            child: Container(
              width: setWidth(100),
              height: setWidth(100),
              child: SizedBox(
                child:
                    Image.asset("assets/images/addpic.png", fit: BoxFit.cover),
              ),
            ),
          )
        ],
      );
    }

    //已选取图片
    return Row(
      children: [
        GestureDetector(
          onTap: () {
            widget.getImage();
          },
          child: Container(
            width: setWidth(100),
            height: setWidth(100),
            child: Image.file(
              widget.fileList[index],
              fit: BoxFit.cover,
            ),
          ),
        )
      ],
    );
  }
}
